package account.student.view;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Rectangle;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import prototyping.client.student.controller.HelpButtonController;
import dao.server.DataLoadIP;
import account.model.SubTeam;
import account.student.controller.StudentController;
import mededelingen.model.Knoppenlijst;

public class StudentViewSmall extends JFrame {

	private static final long serialVersionUID = 1L;  

	private static StudentViewSmall instance;
	private HelpButtonController hbc;

	private JLabel succes;
	private JLabel confirmation;
	private JButton btnBPV;
	private JButton btnDAB;
	private JButton btnPRG;
	private JButton btnPRJ;
	private JButton btnOVR;

	private String userName;
	private String server;

	private TrayIcon trayIcon;
	private SystemTray tray;

	public static StudentViewSmall getInstance() {
		if (instance == null) {
			instance = new StudentViewSmall();
		}
		return instance;
	}

	private StudentViewSmall() {	 
		server = DataLoadIP.getInstance().LoadIP();
		userName = SubTeam.getInstance().getAcc().getPersoon().getVoornaam() + " " + SubTeam.getInstance().getAcc().getPersoon().getAchternaam();
		loadServer(userName, server);

		//jframe laden
		frameSetup();
		//hulpvraagknoppen laden
		loadData();
	}

	private void loadServer(String userName, String server){
		hbc = new HelpButtonController(userName, server);
	}

	private void loadData() {
		//gegevens uit database laden
		Knoppenlijst.getInstance().loadButtons();

		//knop1
		String btn1 = Knoppenlijst.getInstance().getMelding().get(0).getNaam();
		String btn1_2 = Knoppenlijst.getInstance().getMelding().get(0).getBeschrijving();
		//knop2
		String btn2 = Knoppenlijst.getInstance().getMelding().get(1).getNaam();
		String btn2_2 = Knoppenlijst.getInstance().getMelding().get(1).getBeschrijving();
		//knop3
		String btn3 = Knoppenlijst.getInstance().getMelding().get(2).getNaam();
		String btn3_2 = Knoppenlijst.getInstance().getMelding().get(2).getBeschrijving();
		//knop4
		String btn4 = Knoppenlijst.getInstance().getMelding().get(3).getNaam();
		String btn4_2 = Knoppenlijst.getInstance().getMelding().get(3).getBeschrijving();
		//knop5
		String btn5 = "...";

		//hulpvraagknoppen afkorting
		btnPRG.setText(btn1);
		btnPRJ.setText(btn2);
		btnDAB.setText(btn3);
		btnBPV.setText(btn4);
		btnOVR.setText(btn5);

		//hulpvraagknoppen actie
		btnPRG.setActionCommand(btn1_2);
		btnPRJ.setActionCommand(btn2_2);
		btnDAB.setActionCommand(btn3_2);
		btnBPV.setActionCommand(btn4_2);
		btnOVR.setActionCommand("btnOVR");
	}

	private void frameSetup() {	
		//hulpvraagknoppen
		btnPRG = new JButton();
		btnPRG.setBounds(10, 20, 80, 23);
		btnPRG.addActionListener(hbc);

		btnPRJ = new JButton();
		btnPRJ.setBounds(100, 20, 78, 23);
		btnPRJ.addActionListener(hbc);

		btnDAB = new JButton();
		btnDAB.setBounds(188, 20, 80, 23);
		btnDAB.addActionListener(hbc);

		btnBPV = new JButton();
		btnBPV.setBounds(278, 20, 78, 23);
		btnBPV.addActionListener(hbc);

		btnOVR = new JButton();
		btnOVR.setBounds(366, 20, 78, 23);
		btnOVR.addActionListener(new StudentController(btnOVR));

		//success bij verzenden van hulpvraag
		String succes = "Hulpvraag verzonden";

		confirmation = new JLabel(succes);
		confirmation.setBounds(10,0,450,20);
		confirmation.setForeground(Color.blue);
		confirmation.setHorizontalAlignment(SwingConstants.CENTER);
		confirmation.setVerticalAlignment(SwingConstants.CENTER);
		confirmation.setVisible(false);

		//TODO applicatie minimaliseren naar systemtray
		if(SystemTray.isSupported()){
			tray = SystemTray.getSystemTray();

			Image image = new ImageIcon(this.getClass().getResource("/tools/images/Questionmark.png")).getImage();
			PopupMenu popup = new PopupMenu();

			MenuItem open = new MenuItem("Open");
			open.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setVisible(true);
					setExtendedState(JFrame.NORMAL);
				}
			});
			popup.add(open);

			MenuItem sluiten=new MenuItem("Sluiten");
			ActionListener exitListener=new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			};
			sluiten.addActionListener(exitListener);
			popup.add(sluiten);

			trayIcon = new TrayIcon(image, "Project M", popup);
			trayIcon.setImageAutoSize(true);
			trayIcon.addMouseListener(new MouseAdapter() 
			{            
				public void mouseClicked(MouseEvent e) 
				{
					setVisible(true);
					setExtendedState(JFrame.NORMAL);
					StudentViewSmall.getInstance();
				}
			});   
		}else{
			System.out.println("system tray niet mogelijk");
		}
		addWindowStateListener(new WindowStateListener() {
			public void windowStateChanged(WindowEvent e) {
				if(e.getNewState()==ICONIFIED){
					try {
						tray.add(trayIcon);
						setVisible(false);
					} catch (AWTException ex) {
					}
				}
				if(e.getNewState()==7){
					try{
						tray.add(trayIcon);
						setVisible(false);
					}catch(AWTException ex){
					}
				}
				if(e.getNewState()==MAXIMIZED_BOTH){
					tray.remove(trayIcon);
					setVisible(true);
				}
				if(e.getNewState()==NORMAL){
					tray.remove(trayIcon);
					setVisible(true);
				}
			}
		});
		getContentPane().setLayout(null);
		getContentPane().add(confirmation);
		getContentPane().add(btnPRG);
		getContentPane().add(btnPRJ);
		getContentPane().add(btnDAB);
		getContentPane().add(btnBPV);
		getContentPane().add(btnOVR);

		//frame settings
		setTitle("Welkom " + userName);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setResizable(false);
		setVisible(true);
		setSize(464, 80);

		//frame start rechtsonder scherm (windows)
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
		Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
		int x = (int) rect.getMaxX() - this.getWidth();
		int y = (int) rect.getMaxY() - this.getHeight();
		this.setLocation(x, y - 45);
	}

	public void succes(String mess){
		succes = new JLabel(mess);
		succes.setForeground(Color.RED);
		GridBagConstraints gbc_succes = new GridBagConstraints();
		gbc_succes.insets = new Insets(0, 0, 5, 5);
		gbc_succes.gridx = 5;
		gbc_succes.gridy = 3;
		succes.setVisible(true);
		getContentPane().add(succes, gbc_succes);
		revalidate();
	}

	public JLabel getConfirmation(){
		return confirmation;
	}

	public JButton getBtnBPV() {
		return btnBPV;
	}

	public JButton getBtnDAB() {
		return btnDAB;
	}

	public JButton getBtnPRG() {
		return btnPRG;
	}

	public JButton getBtnPRJ() {
		return btnPRJ;
	}

}
